﻿using InnoWeb.CEND.BL.Service.Reports;
using InnoWeb.CEND.Core.Model.Reports;
using InnoWeb.CEND.Data;
using InnoWeb.CEND.Data.Infrastructure;
using InnoWeb.CEND.UI.MVC.Areas.Admin.Models;
using InnoWeb.CEND.Web.Framework;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace InnoWeb.CEND.UI.MVC.Areas.Admin.Controllers
{
    public class ReportController : Controller
    {
        private static WebSiteContext _context;
        private static IUnitOfWork _unitOfWork;

        private static GenericRepository<ReportItem> _reportItemRepository;
        private static GenericRepository<ReportContent> _reportContentRepository;
        private static ReportItemService _reportItemService;
        private static ReportContentService _reportContentService;

        public ReportController()
        {
            _context = new WebSiteContext(ConfigurationManager.ConnectionStrings["CendDBConnection"].ConnectionString);
            _unitOfWork = new UnitOfWork(_context);

            _reportContentRepository = new GenericRepository<ReportContent>(_context);
            _reportItemRepository = new GenericRepository<ReportItem>(_context);

            _reportItemService = new ReportItemService(_unitOfWork, _reportItemRepository);
            _reportContentService = new ReportContentService(_unitOfWork, _reportContentRepository);

        }

        //
        // GET: /Admin/Projects/

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult Index()
        {
            var itemList = _reportItemService.GetAllElements().ToList();
            var contentList = _reportContentService.GetAllElements().ToList();
            List<ReportInfoModel> returnList = new List<ReportInfoModel>();
            var list = (from nI in itemList
                        join nC in contentList on nI.Id equals nC.ReportItemId
                        select new
                        {
                            nI,
                            nC
                        });
            foreach (var item in list)
            {
                ReportInfoModel obj = new ReportInfoModel();
                obj.ReportContent = item.nC;
                obj.ReportItem = item.nI;
                returnList.Add(obj);
            }

            return View(returnList);
        }

        //
        // GET: /Admin/Projects/Details/5

        public ActionResult Details(int id)
        {
            var item = _reportItemService.GetElementById(id);
            var content = _reportContentService.GetAllElements().Where(a => a.ReportItemId == item.Id).FirstOrDefault();

            ReportInfoModel obj = new ReportInfoModel();
            obj.ReportContent = content;
            obj.ReportItem = item;
            return View(obj);
        }

        //
        // GET: /Admin/Projects/Create

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult Create()
        {
            //var itemList = _expertService.GetAllElements().ToList();
            //ViewData["expertList"] = itemList;
            return View();
        }

        //
        // POST: /Admin/Projects/Create

        [HttpPost]
        public ActionResult Create(ReportInfoModel model)
        {
            try
            {
                var img = Request.Files["fileUpload"];

                string fileName = Guid.NewGuid().ToString().Replace("-", "");
                string serverPath = Server.MapPath("~/Content/Files");
                string fullPath = Server.MapPath("~/Content/Files/");

                FileUploader.FileSave(fullPath, img, fileName);

                ReportItem itemObj = model.ReportItem;
                itemObj.CreatedDate = DateTime.Today;
                itemObj.IsActive = true;
                itemObj.IsDeleted = false;
                itemObj.ModifiedDate = DateTime.Today;
                _reportItemService.CreateElement(itemObj);
                _reportItemService.SaveChanges();
                int a = itemObj.Id;

                ReportContent contentObj = model.ReportContent;
                contentObj.CreatedDate = DateTime.Today;
                contentObj.IsActive = true;
                contentObj.IsDeleted = false;
                contentObj.ModifiedDate = DateTime.Today;
                contentObj.ReportUrl = fileName + ".pdf";
                contentObj.ReportItemId = a;
                _reportContentService.CreateElement(contentObj);
                _reportContentService.SaveChanges();

                Response.Write("<script type='text/javascript'>confirm(\"Data successfully added to database.\"); document.location = 'Index';</script>");

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Admin/Projects/Edit/5

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult Edit(int id)
        {
            var item = _reportItemService.GetElementById(id);
            var content = _reportContentService.GetAllElements().Where(a => a.ReportItemId == item.Id).FirstOrDefault();

            ReportInfoModel obj = new ReportInfoModel();
            obj.ReportContent = content;
            obj.ReportItem = item;
            return View(obj);
        }

        //
        // POST: /Admin/Projects/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, ReportInfoModel model)
        {
            try
            {

                var item = model.ReportItem;
                item.ModifiedDate = DateTime.Today;
                _reportItemService.UpdateElement(item);
                _reportItemService.SaveChanges();

                var content = model.ReportContent;
                content.ReportItemId = item.Id;
                content.ModifiedDate = DateTime.Today;

                var img = Request.Files["fileUpload"];

                if (img != null)
                {
                    string fileName = Guid.NewGuid().ToString().Replace("-", "");
                    string serverPath = Server.MapPath("~/Content/Files");
                    string fullPath = Server.MapPath("~/Content/Files/");

                    FileUploader.FileSave(fullPath, img, fileName);

                    content.ReportUrl = fileName + ".pdf";
                }
                else
                {
                    content.ReportUrl = _reportContentService.GetElementById(model.ReportContent.Id).ReportUrl;
                }

                _reportContentService.UpdateElement(content);
                _reportContentService.SaveChanges();

                return View(model);
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Admin/Projects/Delete/5

        //[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        //public ActionResult Delete(int id)
        //{
        //    ProjectsModel mdl = new ProjectsModel();
        //    var newsItem = _reportItemService.GetElementById(id);
        //    var newsContent = _reportContentService.GetAllElements().Where(a => a.WorkshopItemId == id).FirstOrDefault();
        //    mdl.WorkshopContent = newsContent;
        //    mdl.WorkshopItem = newsItem;

        //    return View(mdl);
        //}

        ////
        //// POST: /Admin/Projects/Delete/5

        //[HttpPost]
        //public ActionResult Delete(int id, FormCollection collection)
        //{
        //    try
        //    {
        //        var newsItem = _reportItemService.GetElementById(id);
        //        var newsContent = _reportContentService.GetAllElements().Where(a => a.WorkshopItemId == id).FirstOrDefault();

        //        newsItem.IsDeleted = true;
        //        newsItem.IsActive = false;

        //        newsContent.IsDeleted = true;
        //        newsContent.IsActive = false;

        //        _reportItemService.UpdateElement(newsItem);
        //        _reportItemService.SaveChanges();

        //        _reportContentService.UpdateElement(newsContent);
        //        _reportContentService.SaveChanges();

        //        return RedirectToAction("Index");
        //    }
        //    catch
        //    {
        //        return View();
        //    }
        //}
    }
}
